package com.situ.company.department.service.impl;

import java.util.Arrays;
import java.util.List;

import com.situ.company.department.dao.IDepartmentDao;
import com.situ.company.department.dao.impl.DepartmentDaoImpl;
import com.situ.company.department.model.DepartmentModel;
import com.situ.company.department.service.IDepartmentService;
import com.situ.company.employee.dao.IEmployeeDao;
import com.situ.company.employee.dao.impl.EmployeeDaoImpl;
import com.situ.company.employee.model.EmployeeModel;
import com.situ.company.util.FmtEmpty;

public class DepartmentServiceImpl implements IDepartmentService {
	private IDepartmentDao dao=new DepartmentDaoImpl();
	private IEmployeeDao employeeDao=new EmployeeDaoImpl();
	public String insert(DepartmentModel model) {
		DepartmentModel m0=new DepartmentModel();
		m0.setCode(model.getCode());
		if(selectModel(m0)==null) {
			Integer res=dao.insert(model);
			return res==null?null:res.toString();
		}
		return "repeat";
	}
	/**
	 *部门编号下存在员工，不能删除
	 */
	public String delete(DepartmentModel model) {
		EmployeeModel m1=new EmployeeModel();
		m1.setCodeDept(model.getCode());
		System.out.println(model.getCode());
		List<EmployeeModel> list = employeeDao.selectList(m1);
		
		System.out.println(list.size());
		
		if(FmtEmpty.isEmpty(list)) {
			Integer res=dao.delete(model);
			return res==null?null:res.toString();
		}else {return "exist";}
		
	}
	public String update(DepartmentModel model) {
		return dao.updateActive(model)+"";
	}
	public DepartmentModel selectModel(DepartmentModel model) {
		DepartmentModel m0=new DepartmentModel();
		m0.setCode(model.getCode());
		return dao.selectModel(m0);
	}
	public List<DepartmentModel> selectList(DepartmentModel model) {
		String code=model.getCode();
		model.setCode(code==null?"%%":"%"+code+"%");
		String name=model.getName();
		model.setName(name==null?"%%":"%"+name+"%");
		return dao.selectList(model);
	}
	
	public Integer selectCount(DepartmentModel model) {
		DepartmentModel m0=new DepartmentModel();
		String code=model.getCode();
		model.setCode(code==null?"%%":"%"+code+"%");
		String name=model.getName();
		model.setName(name==null?"%%":"%"+name+"%");
		return dao.selectCount(m0);
	}
}
